package com.fanco.learn_datastruct_leetcode.juc;/**
 * @author qiaowang 2021/8/19
 */

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author fanco
 * @Desc CyclicBarrierDemo
 */
public class CyclicBarrierDemo {
    public static void main(String[] args) {
        // 通过配置 数量设置必须多少个线程时 才能执行 后续操作，操作定义在 CyclicBarrier的第二个参数中
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7, () -> {
            System.out.println("集齐龙珠 召唤神龙");
        });

        for (int i = 1; i <= 7; i++) {
            int finalI = i;
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + " 线程启动");
                System.out.println("收集到第" + finalI +"颗龙珠");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }

            }, "线程" + i).start();
        }
    }
}
